<template>
  <!-- 盘盈列表 -->
  <div class="content">
    <jc-button
      :btn-arr="['reload', 'newy', 'remove', 'sub', 'pass', 'option', 'batchFill']"
      :disab-arr="['using', 'close', 'cancel', 'com1', 'com2', 'chart', 'freeze']"
      url="tStkStkcountGain/queryStkcountGainList"
      @refresh="handleGetUntreated"
      @toExcel="handleExport"
      @newly="newAssem"
      @submit="submit(1)"
      @noSubmit="submit(2)"
      @audit="audit(1)"
      @noAudit="audit(2)"
      @del="delectHandle"
    />
    <div class="header">
      <div class="header-name">
        <search
          :options="selectData"
          :search-son="searchSon"
          :msg="tacitly"
          @seek="collect"
          @hand="handleQuerySonClass"
        />
        <el-button type="primary" class="btn" size="mini" @click="handleQuerySonClass">{{ $t('btn.search') }}</el-button>
        <filtration
          :ftype="ftype"
          :msg="tacitly"
          :select-data="selectData"
          :table-header="tableHeader1"
          :detail-header="tableHeader"
          @seachData="handleQuerySonClass"
        />
      </div>
    </div>
    <div>
      <jc-table
        v-loading="loading"
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        sortable
        table-selection
        @selectionChange="handleSelectAll"
      >
        <!--审核状态-->
        <template v-slot:btnStates="col">
          <span v-if="col.scope.row.fdocumentstatus === ''" />
          <el-tag
            v-else
            :type="col.scope.row.stateColor"
          >
            {{ col.scope.row.fdocumentstatus }}
          </el-tag>
        </template>
        <template v-slot:tagSlot="col">
          <el-link type="primary" @click="jumpMateriel(col.scope.row)">{{ col.scope.row.fnumber }}</el-link>
        </template>
        <template v-slot:billSlot="col">
          <el-link type="primary" @click="pushView(col.scope.row)">{{ col.scope.row.fbillno }}</el-link>
        </template>
      </jc-table>
      <cardTab @search="handleQueryBomList" />
    </div>
    <!--    分页器-->
    <div class="footer">
      <jc-pagination
        :total="total"
        :page.sync="currentPage"
        :limit.sync="size"
        @pagination="handleGetUntreated"
      />
    </div>
  </div>
</template>

<script>
import {
  queryStkcountGainList
} from '@/api/repertory/stock'
import search from '@/components/Search'
import searData from '@/components/Search/mixin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import filtration from '@/components/filtration'
import jcButton from '@/components/Button'
import {
  deleteErpGain,
  notreviewErpGain,
  notsubmitErpGain,
  reviewErpGain,
  submitErpGain
} from '@/api/repertory/picking/otherWarehousing'
import { toMxBmina } from '@/components/ToMxamineState'

export default {
  // name: 'ForthwithRep',
  components: {
    cardTab: () => import('@/components/cardTab'),
    search,
    jcButton,
    filtration
  },
  mixins: [searData],
  data() {
    return {
      ftype: 88,
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 200, // 每页显示多少条数据
      // 表头
      tableData: [], // 销售数据
      tableHeight: '73vh',
      tableHeader: [
        { label: this.$t('market.fdate'), prop: 'fdate', width: '120' },
        { label: this.$t('market.billNo'), type: 'bill', prop: 'fbillno', width: '120' },
        { label: this.$t('market.documentstatus'), type: 'states', prop: 'fdocumentstatus', width: '100px' },
        { label: this.$t('market.fmodel'), prop: 'fmodel', width: '120' },
        { label: this.$t('market.fmaterial'), type: 'tag', prop: 'fnumber', width: '120' },
        { label: this.$t('shop.fname2'), prop: 'fname', width: '120' },
        { label: this.$t('shop.fspecificaTion'), prop: 'fspecification', width: '140' },
        { label: this.$t('shop.fdescripTion'), prop: 'fdescription', width: 200 },
        { label: this.$t('market.funit'), prop: 'funit', width: '80' },
        { label: this.$t('market.gainqty'), prop: 'fqty', width: '80' },
        { label: this.$t('market.valueOf'), prop: 'fworth', width: '80' },
        { label: this.$t('market.stockname'), prop: 'fstockDesc' },
        { label: this.$t('market.stockNumber'), prop: 'fstockCode' },
        { label: this.$t('market.operationOrder'), prop: 'fsourcebillno', width: '110' },
        { label: this.$t('market.createDate'), prop: 'fcreatedate' }
      ],
      tableHeader1: [
        { label: this.$t('market.creator'), prop: 'fcreator', width: '100' },
        { label: this.$t('market.modifier'), prop: 'modifier' },
        { label: this.$t('market.modifyDate'), prop: 'modifydate' }
      ]
    }
  },
  created() {
    this.handleGetUntreated()
    this.tableHeader1 = this.tableHeader.concat(this.tableHeader1)
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 240px)' : '70vh'
  },
  methods: {
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else {
          queryStkcountGainList({
            pageSize: 100000,
            pageNum: 1,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.array
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, 'ERP盘盈列表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取入库数据
    async handleGetUntreated(ev) {
      this.loading = true
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = {
        pageNum: this.currentPage,
        pageSize: this.size,
        search: this.searCollData.concat(this.details)
      }
      const RES = await queryStkcountGainList(DATA)
      this.tableData = RES.data.array.map(item => {
        return toMxBmina(item)
      })
      this.total = RES.data.total
      this.loading = false
    },
    // 新增
    newAssem() {
      this.$router.push({ path: '/inventoryAddERP' })
    },
    pushView(ev) {
      this.$router.push({ path: `/inventoryDetailERP/${ev.fid}` })
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    },
    // 成品 原材料
    handleQueryBomList(ev) {
      this.searCollData = []
      this.currentPage = 1
      this.searCollData = ev
      this.searchSon = ev
      this.handleGetUntreated()
    },
    // 全选
    handleSelectAll(item) {
      this.selectAll = item
      this.fbillNo = item[0].fbillNo
      this.listDate = []
      item.forEach(item => {
        item.fbillno && this.listDate.push(item.fbillno)
      })
    },
    // 公共函数
    toExamineFun(api) {
      const DATA = { Numbers: this.listDate }
      api(DATA).then(res => {
        if (res.code !== 0) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.handleGetUntreated()
        this.listDate = []
        this.innerHTM = ''
        res.data.forEach(res => {
          this.innerHTM += '<div>' + res + '</div>'
        })
        this.$notify.warning({
          title: '提示',
          message: this.innerHTM,
          duration: 3000, offset: 50,
          dangerouslyUseHTMLString: true
        })
      })
    },
    // 提交
    async submit(ev) {
      const api = ev === 1 ? submitErpGain : notsubmitErpGain
      this.toExamineFun(api)
    },
    // 审核
    async audit(ev) {
      const api = ev === 1 ? reviewErpGain : notreviewErpGain
      this.toExamineFun(api)
    },
    // 删除
    delectHandle() {
      this.$confirm('此操作将永久删除该订单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        dangerouslyUseHTMLString: true,
        type: 'warning'
      }).then(() => {
        const api = deleteErpGain
        this.toExamineFun(api)
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.content {
  @include listBom;
  .el-form {
    .el-form-item {
      margin-bottom: 10px;
    }
  }
  .header{
    position:relative;
    margin-bottom: 5px;
    .header-name{
      width: 100%;
    }
    .btn{
      transform: translateY(18%);
      margin-left: 450px !important;
    }
  }
}
</style>
